#ifndef VPROJECTTREEWIDGET_H
#define VPROJECTTREEWIDGET_H

#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QLine>
#include <QRect>
#include <QMenu>
#include <QDir>
#include <QJsonObject>

class VProjectTreeWidget : public QTreeWidget
{
    Q_OBJECT
public:
    enum TreeWidgetItemType
    {
        FILE,
        PROJECT,
        FOLDER,
        CUSTOM_FOLDER,
        EXTERNAL_FOLDER,
        MODULAR_FOLDER,
        ATTRIBUTE,
        MODULAR
    };

    struct VItemData
    {
        int type;
        int line;
        QString path;
        VItemData(){}
        VItemData(int type, QString path, int line)
        {
            this->type = type;
            this->path = path;
            this->line = line;
        }
    };

    QString _active_project;

    explicit VProjectTreeWidget(QWidget *parent = 0);
    void open_project(QString esln_path);

    QString get_current_item_path();
    QString get_current_project_path();
    QString get_project_esln_path(QTreeWidgetItem *item);
    QString get_item_path(QTreeWidgetItem *item);
    int get_current_item_type();
    void access_file_homepage();
    bool set_active_project(QString esln_path);
    void close_project();
    void export_file();
    void init();
    void remove_project(QTreeWidgetItem *item);
    void on_project_attribute();
    void on_project_pack();

signals:
    void signal_open_file(QString path);
    void signal_rename(QString old_path, QString new_path);
    void signal_project_attribute_changed(QString esln_path);
    void signal_access_file_homepage(QString path);
    void signal_open_project(QString path);
    void signal_close_project(QString path);
    void signal_reload_project(QString path);
    void signal_jump(QString path, int line);
	void signal_delete_file(QString path);

public slots:
    void slot_update_outline(QString file_path, QList<QTreeWidgetItem *> list);
    void slot_update_project_external_dependence(QString esln_path, QList<QTreeWidgetItem *> list);


private:
    QRect m_rect;
    bool m_move = false;
    void dragEnterEvent( QDragEnterEvent* event );
    void dragLeaveEvent( QDragLeaveEvent* event );
    void dragMoveEvent( QDragMoveEvent* event );
    void dropEvent( QDropEvent* event );
    void startDrag(Qt::DropActions supportedActions);
    void paintEvent(QPaintEvent *event);
    bool event(QEvent *e);
    QTreeWidgetItem *_darg_item = nullptr;
    QTreeWidgetItem *_darg_to_item = nullptr;
    int _darg_item_type;
    int _darg_to_item_type;

    VItemData get_item_data(QTreeWidgetItem * item);
    void set_item_data(QTreeWidgetItem * item, int type, QString path);
    QJsonObject get_prject_custom_folder(QTreeWidgetItem *item);
    void recursion_item(QTreeWidgetItem *item, QJsonArray &arr, QDir &dir);
    QTreeWidgetItem *get_top_item(QTreeWidgetItem *item);
    bool is_item_contain(QTreeWidgetItem *item, QString text, int type);
    void update_esln(QTreeWidgetItem *item);
    QTreeWidgetItem* get_project_folder(QTreeWidgetItem *project, int type);
    QTreeWidgetItem* get_project_item(QString path);
    bool is_custom_folder_child(QTreeWidgetItem *item);

    bool m_comtain;
    bool is_project_contain_file(QTreeWidgetItem *item, QString text);
    void recursion_project_contain(QTreeWidgetItem *item, QString text);
    QString get_project_source_path(QTreeWidgetItem *item);
    void sort_item_child(QTreeWidgetItem *item);
    QList<QTreeWidgetItem*> sort_item_list(QList<QTreeWidgetItem*> list);
    int get_member_of_list_index(int member, QList<int> &list);


    bool is_parent(QTreeWidgetItem *parent_item, QTreeWidgetItem *item);
    bool is_project_open(QString path);
    void recursion_catalog(QTreeWidgetItem *item, QJsonObject &root, QDir &dir);
    void recursion_project(QTreeWidgetItem *item, QJsonArray &root, QDir &dir);
    bool apped_historical_record(QString path);
    void set_file_item_icon(QTreeWidgetItem *item);
    void contextMenuEvent(QContextMenuEvent *);

    QMenu _content_menu;
    QMenu _append_menu;
private slots:
    void on_doubleClicked();
//    void on_currentItemChanged(QTreeWidgetItem *item);
    void on_new_folder();
    void on_new_file();
    void on_add_file();
    void on_export_file();
    void on_delete();
    void on_rename();
    void on_set_project_active();
    void on_setting_module();
};

Q_DECLARE_METATYPE(VProjectTreeWidget::VItemData);

#endif // VPROJECTTREEWIDGET_H
